import Vue from 'vue';
import http from './http';
import store from './store/index';
import router from './router/index';
import Root from './Root.vue';
import axios from 'axios';
import qs from 'Qs';

// 引入公共样式表
import 'normalize.css';
import 'animate.css';

import './assets/styles/base.scss';

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


//兼容IE11
if (!FileReader.prototype.readAsBinaryString) {
    FileReader.prototype.readAsBinaryString = function(fileData) {
        var binary = "";
        var pt = this;
        var reader = new FileReader();
        reader.onload = function(e) {
            var bytes = new Uint8Array(reader.result);
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            pt.content = binary;
            pt.onload()
        }
        reader.readAsArrayBuffer(fileData);
    }
}

UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function(action) {
    if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
        return "http://www.h3clearning.com/huathree-back/uploadUeFile";
    } else if (action == 'uploadvideo') {
        return "http://www.h3clearning.com/huathree-back/uploadUeFile";
    } else {
        return this._bkGetActionUrl.call(this, action);
    }
};


// 引入echarts主模块（基础模块）
import ECharts from 'vue-echarts/components/ECharts.vue'

//引入富文本
import VueUeditorWrap from 'vue-ueditor-wrap'
import '../static/UEditor/ueditor.config.js'
import '../static/UEditor/ueditor.all.min.js'
import '../static/UEditor/lang/zh-cn/zh-cn.js'
import '../static/UEditor/ueditor.parse.min.js'

import 'src/assets/styles/base.scss';
import 'src/assets/styles/common.scss';
import 'src/assets/styles/iconfont.css';

import 'src/assets/font/iconfont.css'
Vue.use(ElementUI);

Vue.component('chart', ECharts);

Vue.config.productionTip = false;

Vue.component('vue-ueditor-wrap', VueUeditorWrap)

// 将axios挂载到prototype上，在组件中可以直接使用this.axios访问
Vue.prototype.axios = http;
Vue.prototype.qs = qs;

//新增
Vue.prototype.$http = axios;

axios.withCredentials = true;
Vue.prototype.$ajax = function(options) {
    if (this.$route.path != '/login') {
        if (options.headers != null) {    
            options.headers.userid = this.$root.userInfoMsg.id,
                options.headers.Authorization = this.$root.userInfoMsg.sessionId  
        };
    }

    options = options || {};
    this.$http({
        url: options.url,
        data: options.data || {},
        headers: options.headers || { 'Content-type': 'application/x-www-form-urlencoded;charset=utf-8', 'userid': this.$root.userInfoMsg.id, 'Authorization': this.$root.userInfoMsg.sessionId },
        method: options.method || 'GET',
    }).then((res) => {
        if (options.success) {
            options.success(res)
        }
    }).then((complete) => {
        if (options.complete) {
            options.complete(complete)
        }
    }).catch((err) => {
        if (options.fail) {
            options.fail(err)
        }
    })
};

new Vue({
    el: '#app',
    store,
    router,
    template: '<Root/>',
    components: { Root },
    data() {
        return {
            // jifuUrl: 'http://www.h3clearning.com/huathree-back',
            // jifuUrl: 'http://192.168.31.132:8091', //海峰  
            jifuUrl: 'http://192.168.31.129:8091', //长城的本地服务器地址  
            platformUrl: 'http://www.h3clearning.com/huathree-back/getAllPlatformName',
            //platformUrl:'http://192.168.14.111:8091/getAllPlatformName',
            managerMsg: {},
            columnMsg: {},
            versionMsg: {},
            newMsg: {},
            teacherMsg: {},
            userMsg: {},
            enterpMsg: {},
            goodsMsg: {},
            assignMsg: {},
            bannerMsg: {},
            imgGuanLiMsg: {},
            compulsoryMsg: {},
            assignCourseMsg: {},
            lectureMsg: {},
            adminMsg: {},
            platformMsg: {},
            assignDepartmentMsg: [],
            liveMsg: {},
            liveColumn_Msg: {},

            columnToNewLive: {},
            homeworkMsg: {},
            videoMsg: {},
            fileMsg: {},
            studyMapMsg: {},
            courseTaskManagerMsg: {},
            auditMsg: {},
            satisfactionMsg: {},
            xieyiMsg: {},
                  assignSaveType: 'add',
            userInfoMsg: null,
            huasan: {},


            isopen: 0
        }
    },
    mounted: function() {      
        if (this.$root.userInfoMsg == null) {          
            this.$store.commit('GET_USERINFOMSG');          
            this.$root.userInfoMsg = this.$store.state.userInfoMsg;
            //console.log("刷新后：",this.$root.userInfoMsg);
                       //console.log("刷新后公司Id：",this.$root.userInfoMsg.companyid,this.$root.userInfoMsg.id)
                  
        }   
    },
    watch: {
        '$route' (to, from) {
            //			console.log('watch:',to.path, from.path)
        }
    }
});

Vue.prototype.$rootFunction = function() {
    return {
        rootDelete: (transitionRow) => {
            let that = this;
            that.$refs.deleteAlert.show({
                alertText: '是否确认删除？',
                alertSubmit: function() {
                    alertStatus: 0;
                    that.deleteSubmit();
                }
            });
            console.log('执行删除完毕!');
        },
        rootDeleteAll: () => {
            let that = this;
            this.deleteType = 'Batch';
            if (this.multipleSelection.length == 0) {
                this.$refs.alert.show({
                    alertText: '批量选择为空',
                    alertSubmit: function() {
                        alertStatus: 0
                    }
                });
            } else {
                this.$refs.deleteAlert.show({
                    alertText: '是否确认删除？',
                    alertSubmit: function() {
                        alertStatus: 0;
                        that.deleteSubmit();
                    },
                    alertCancel: function() {
                        alertStatus: 0;
                    }
                });
            }
        },
        deleteInTableList: () => {
            let that = this;
            setTimeout(() => {
                that.$refs.alert.hide();
                that.getTableList();
            }, 2000)
        },
        deleteResSuccess: () => {
            let that = this;
            that.$refs.alert.show({
                alertText: '删除成功',
                alertSubmit: function() {
                    alertStatus: 0;
                    that.getTableList();
                }
            });
            that.$rootFunction().deleteInTableList();
        },
        deleteResFail: () => {
            let that = this;
            that.$refs.alert.show({
                alertText: that.failText,
                alertSubmit: function() {
                    alertStatus: 0
                }
            });
        },
        cancelTuijianResSuccess: () => {
            //			取消失败与刷新 与删除保持一致
            let that = this;
            that.$refs.alert.show({
                alertText: '取消推荐成功',
                alertSubmit: function() {
                    alertStatus: 0;
                    that.getTableList();
                }
            });
            that.$rootFunction().deleteInTableList();
        },
        rootRelease: (transitionRow) => {
            let that = this;
            that.$refs.deleteAlert.show({
                alertText: '是否确认发布？',
                alertSubmit: function() {
                    alertStatus: 0;
                    that.fabuSubmit();
                }
            });
            console.log('执行发布完毕!');
        },
        releaseResSuccess: () => {
            let that = this;
            that.$refs.alert.show({
                alertText: '发布成功',
                alertSubmit: function() {
                    alertStatus: 0;
                    that.getTableList();
                }
            });
            that.$rootFunction().deleteInTableList();
        },
        addResSuccess: () => {
            let that = this;
            that.$refs.alert.show({
                alertText: '保存成功',
                alertSubmit: function() {
                    alertStatus: 0;
                    that.getTableList();
                }
            });
            that.label_wrap = false;
            that.$rootFunction().deleteInTableList();
        },

        getRootCompangIdStatus: (suc, options) => {
            this.$ajax({
                url: this.$root.jifuUrl + "/getPlatformRoleLabelInfo?" + 'rootcompanyid=' + 1,
                method: 'GET',
                data: {},
                success: res => {
                    //					suc(res);
                    if (res.data.code == 200) {
                        this.isopen = res.data.data.isopen;
                        console.log('加载平台是否开启', this.isopen);
                    }
                }
            });
        },
        getTeacherLevelStatus: (suc) => {
            this.$ajax({
                url: this.$root.jifuUrl + "/getGradeRuleOpenClose?" + 'rootcompanyid=' + 1,
                method: 'GET',
                data: {},
                success: res => {
                    if (res.data.code == 200) {
                        this.teacherIsOpen = res.data.data.isopen;
                        console.log('加载讲师等级是否开启', this.teacherIsOpen);
                    }
                }
            });
        },
        getTeacherLevelData: (suc) => {
            this.$ajax({
                url: this.$root.jifuUrl + "/getGradeRulePage?" + 'rootcompanyid=' + 1 + '&type=2',
                method: 'GET',
                data: {},
                success: res => {
                    if (res.data.code == 200) {
                        res.data.data.forEach(element => {
                            this.gradeRuleIdArr.push({ id: element.id, name: element.name });
                        });
                    }
                }
            });
        },
        getAllLecture: (suc) => {   
            this.$ajax({                    
                url: this.$root.jifuUrl + "/getAllLecture?" + 'rootcompanyid=' + 1,
                            method: 'GET',
                            data: {},
                            success: res => {                 
                    if (res.data.code == 200) {
                        this.lectureoption = res.data.data;
                        this.lectureoptionCopy = res.data.data;                  
                    }                    
                }                
            });  
        },
        getAllCity: (suc) => {   
            this.$ajax({                
                url: this.$root.jifuUrl + "/getProviceCityRegionInfo",
                                method: 'GET',
                                data: {},
                                success: res => {                    
                    if (res.data.code == 200) {
                        console.log('获取所有省市**********************');
                        this.cityArr = res.data.data;
                        this.cityArr.unshift({ id: '', name: "选择地区", list: [] }); 
                        this.cityArr.forEach(element => {
                            element.list = this.getTreeDataCity(element.list);
                        });                    
                    }                
                }            
            });
        },
        columnList: (options) => {
            this.$ajax({
                url: this.$root.jifuUrl + "/mns/kcColumn/kcColumnByColumntype",
                headers: { 'ContentType': 'application/json; charset=utf-8' },
                method: 'POST',
                data: { columnTypes: options.columnType, rootcompanyid: 1 },
                success: res => {
                    if (res.data.code == 200) {
                        let columnArr = res.data.data;
                        columnArr.unshift({ childList: [], columnName: "课程分类", id: '' });
                        this.options = this.getTreeData(columnArr);
                        console.log('已加载栏目列表**********************options');
                    }
                }
            });
        },
        columnListAdd: (options) => {
            this.$ajax({
                url: this.$root.jifuUrl + "/mns/kcColumn/kcColumnByColumntype",
                headers: { 'ContentType': 'application/json; charset=utf-8' },
                method: 'POST',
                data: { columnTypes: options.columnType, rootcompanyid: 1 },
                success: res => {
                    if (res.data.code == 200) {
                        this.KcColumnYi = res.data.data;
                        this.KcColumnYi.forEach(element => {                                
                            if (element.id == this.courseColumnYi) {                                    
                                this.KcColumnEr = element.childList                                
                            }                            
                        });
                        console.log('已加载栏目列表add**********************options');
                    }
                }
            });
        },
        tagType: (options) => {            
            this.$ajax({                
                url: this.$root.jifuUrl + "/getLabelByType?" + 'type=' + options.tagType + '&rootcompanyid=' + 1,
                                method: 'GET',
                                data: {},
                                success: res => {                    
                    if (res.data.code == 200) {
                        this.permissionArr = res.data.data;
                        console.log('获取列表观看权限**********************options');                    
                    }                
                }            
            });
        },
        userPermissions: (options) => {
            this.$ajax({                
                url: this.$root.jifuUrl + "/mns/label/labelListByExample",
                headers: { 'ContentType': 'application/json; charset=utf-8' },
                                method: 'POST',
                                data: { type: options.permissType, rootcompanyid: 1 },
                                success: res => {                    
                    if (res.data.code == 200) {
                        if (options.permissType == 3) {
                            this.subjects = res.data.data;
                            console.log('课程标签3:', this.subjects);
                        } else if (options.permissType == 4) {
                            this.optionLevel = res.data.data;
                            console.log('观看权限4:', this.optionLevel);
                            this.optionLevel.forEach(element => {                     this.cityOptions.push(element.name);                 });
                        } else if (options.permissType == 6) {
                            this.knowledgeMapTypeIdArr = res.data.data;
                            console.log('知识体系6:', this.knowledgeMapTypeIdArr);
                        } else if (options.permissType == 7) {
                            this.knowledgeMapTypeIdArr = res.data.data;
                            console.log('话题分类7:', this.knowledgeMapTypeIdArr);
                        }                    
                    }                
                }            
            })
        },



    }
}